<?php
/**
 * Created by PhpStorm.
 * User: zhoujinda
 * Date: 2015/12/17
 * Time: 9:46
 */

namespace Api\Domain;


use Admin\Api\PhoneDeviceApi;
use Uclient\Api\UserApi;

class DeviceDomain extends BaseDomain
{
    protected $business_code = 'device';

    protected $allowType = array("json", "rss", "html");

    /**
     * 用户手机设备绑定
     */
    public function bind(){

        $this->checkVersion(100);

        $notes = "应用" . $this->client_id . "，调用用户手机设备绑定接口";

        addLog("Device/bind", $_GET, $_POST, $this->client_id . $notes);

        if(IS_POST){

            $device_token = $this->_post('device_token','','设备的唯一标志缺失');
            $uid =  $this->_post('uid','','用户ID缺失');
            $version = $this->_post('version','','设备版本缺失');
            $device_type = $this->_post('device_type','','手机类型缺失');
            $phone_type = $this -> _post('phone_type',"");

            $device_type =  strtolower($device_type);

            //CHECK用户ID
            $result = apiCall(UserApi::GET_INFO,array($uid));
            if(!$result['status']){
                $this->apiReturnErr("用户不存在!");
            }

            //CHECK ID与device_token
            $map['uid']=$uid;
            $map['device_token']=$device_token;
            $result = apiCall(PhoneDeviceApi::COUNT,array($map));


            //entity
            $entity = array(
                'device_token'=>$device_token,
                'uid'=>$uid,
                'version'=>$version,
                'device_type'=>$device_type,
                'phone_type'=>$phone_type,
            );

            if($result['status']){
                if($result['info']!=0){
                    $result = apiCall(PhoneDeviceApi::SAVE,array($map,$entity));
                    if($result['status']){
                        $this->apiReturnSuc("绑定成功");
                    }else{
                        $this->apiReturnErr($result['info']);
                    }
                }
            }else{
                $this->apiReturnErr($result['info']);
            }

            //INSERT

            $result = apiCall(PhoneDeviceApi::ADD,array($entity));

            if($result['status']){
                $this->apiReturnSuc("绑定成功");
            }else{
                $this->apiReturnErr($result['info']);
            }

        }else{
            $this->apiReturnErr("只支持POST请求!");
        }

    }


}